<?php
/**
 * Created by PhpStorm.
 * User: qixiuxiu
 * Date: 2017/4/6
 * Time: 15:30
 */
require_once __DIR__ . '/../vendor/autoload.php';//包容这个文件，可以允许类被自动加载

use Symfony\Component\HttpFoundation\Request;//引入请求类
use Symfony\Component\HttpFoundation\Response;//引入返回类
use Symfony\Component\Routing;//引入routing路由
use Symfony\Component\HttpKernel;//引入控制器解析器
use Symfony\Component\EventDispatcher\EventDispatcher;//引事件派遣类
//在前端控制器中创建dispatcher，然后注册一个监听到response事件
$dispatcher = new EventDispatcher();
//addListener()关联了一个指向了“命名事件”（response）的有效PHP回调。事件名称必须和使用在dispatch()方法中的那个一样。
$dispatcher->addListener('response',array(new Simplex\ContentLengthListener(),'onResponse'),-255);
$dispatcher->addListener('response',array(new Simplex\GoogleListener(),'onResponse'));
//$dispatcher->addListener('response',function (Simplex\ResponseEvent $event){
//   $response = $event->getResponse();
//    //设置响应的Content-Length
//    $headers = $response->headers;
//
//    if (!$headers->has('Content-Length') && !$headers->has('Transfer-Encoding')) {
//        $headers->set('Content-Length', strlen($response->getContent()));
//    }
//
//    //仅在响应“没有被重定向”、请求的格式（format）是HTML并且响应的content-type也是HTML的时候,添加了Google统计代码
//    if ($response->isRedirection()
//        || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
//        || 'html' !== $event->getRequest()->getRequestFormat()){
//        return;
//    }
//    $response->setContent($response->getContent().'GA CODE');
//});
/*处理请求的逻辑分离至Simplex\Framework类*/
$request = Request::createFromGlobals();
$routes = include __DIR__ . '/../src/app.php';

$context = new Routing\RequestContext();
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);

$controllerResolver = new   Symfony\Component\HttpKernel\Controller\ControllerResolver();
$argumentResolver = new Symfony\Component\HttpKernel\Controller\ArgumentResolver();

$framework = new Simplex\Framework($dispatcher,$matcher,$controllerResolver,$argumentResolver);
$response = $framework->handle($request);

//$response->send();